const express = require('express');

// 实体类
const Department = require('../entity/hospital/Department');
const UpdateDepartment = require('../entity/hospital/updateDepartment');
const User = require('../entity/user/User');
const ObjId = require('../entity/common/ObjId');
const Response = require('../entity/http/Response');

// 业务层
const hospitalService = require('../service/hospital');

const router = express.Router();

/**
 * @apiName 创建医院科室并注册负责人接口
 * @param { string } hospital       医院名称
 * @param { string } department     科室名称
 * @param { string } departmentID   科室ID
 * @param { int }    cityId         城市ID
 * @param { string } name           负责人真实姓名
 * @param { string } userType       负责人用户类型
 * @param { string } phone          负责人手机号
 * @param { string } account        负责人账号
 * @param { string } password       负责人密码
 * @pseudocode 运行逻辑:
 *                 1. 将负责人添加到 user 表中
 *                 2. 创建医院科室
 */
router.post('/hospital-adduser', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /hospital/hospital-adduser`);
    const { uid } = req.token_payload.data;
    let department_data;
    let user;
    // 读取科室数据
    try {
        department_data = new Department(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室参数 ${ JSON.stringify(department_data) }`);
    // 读取负责人用户数据
    try {
        user = new User(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "负责人用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 负责人参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    hospitalService.addHospitalAndAdmin(req.requestId, uid, department_data, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 创建科室响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 创建科室时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 创建医院科室接口
 * @param { string } hospital       医院名称
 * @param { string } department     科室名称
 * @param { string } departmentID   科室ID
 * @param { int }    cityId         城市ID
 * @param { int }    adminId        负责人ID
 * @pseudocode 运行逻辑:
 *                 1. 创建医院科室
 */
router.post('/hospital', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /hospital/hospital`);
    const { uid } = req.token_payload.data;
    let department_data;
    let user;
    // 读取科室数据
    try {
        department_data = new Department(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室参数 ${ JSON.stringify(department_data) }`);
    // 读取负责人信息
    try {
        user = new ObjId(req.body.adminId);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "负责人用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 负责人参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    hospitalService.addHospital(req.requestId, uid, department_data, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 创建科室响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 创建科室时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 删除医院科室接口
 * @param { string } departmentId   科室ID: Department 表的主键
 * @pseudocode 运行逻辑:
 *                 1. 逻辑删除 Department 表 中对应的科室
 */
router.delete('/hospital', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /hospital/hospital`);
    const { uid } = req.token_payload.data;
    let department;
    // 读取负责人信息
    try {
        department = new ObjId(req.query.id);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室参数 ${ JSON.stringify(department) }`);
    // 进入业务层
    hospitalService.delHospital(req.requestId, uid, department).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除科室响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除科室时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改医院科室信息接口
 * @param { int }    departmentId   科室ID - 主键
 * @param { string } hospital       医院名称
 * @param { string } department     科室名称
 * @param { int }    cityId         城市ID
 * @param { int }    adminId        负责人ID
 * @pseudocode 运行逻辑:
 *                 1. 直接修改科室表即可
 */
router.put('/hospital', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /hospital/hospital`);
    const { uid } = req.token_payload.data;
    let department;
    let department_data;
    let user;
    // 读取科室数据
    try {
        department = new ObjId(req.body.departmentId);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室Id ${ JSON.stringify(department) }`);
    try {
        department_data = new UpdateDepartment(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室参数 ${ JSON.stringify(department_data) }`);
    // 读取负责人信息
    try {
        user = new ObjId(req.body.adminId);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "负责人用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 负责人参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    hospitalService.changeHospital(req.requestId, uid, department, department_data, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改科室信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改科室信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取医院科室信息接口
 * @param { int }    id     科室表的主键id
 * @pseudocode 运行逻辑:
 *                 1. 直接查数据库后返回信息即可
 */
router.get('/hospital', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /hospital/hospital`);
    const { uid } = req.token_payload.data;
    let department;
    // 读取科室数据
    try {
        department = new ObjId(req.query.id);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室Id ${ JSON.stringify(department) }`);
    // 进入业务层
    hospitalService.getHospital(req.requestId, uid, department).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取科室信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取科室信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取全量医院科室接口
 * @pseudocode 运行逻辑:
 *                 1. 直接查数据库后返回信息即可
 */
router.get('/allHospital', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /hospital/allHospital`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    hospitalService.getAllHospital(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有科室响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有科室时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 懒加载医院科室接口
 * @param { int }   cursor   即将获取的科室ID
 * @param { int }   limit    即将获取的科室数量
 * @pseudocode 运行逻辑:
 *                 1. 只是在全量返回的基础上增加了懒加载的 cursor 和 limit 而已
 */
router.get('/allHospitalLazyload', function (req, res, next) {});

/**
 * @apiName 分页获取医院科室接口
 * @param { int }   pn   分页的页数(从1算起)
 * @param { int }   ps   每一页的数量
 * @pseudocode 运行逻辑:
 *                 1. 只是在全量返回的基础上增加了分页逻辑的 pn 和 ps 而已
 */
router.get('/allHospitalLazyload', function (req, res, next) {});

/**
 * @apiName 模糊搜索返回全量符合条件的医院科室接口
 * @param { int }       cityId       城市ID - 主键
 * @param { string }    hospital     医院名称
 * @param { string }    department   科室名称
 * @pseudocode 运行逻辑:
 *                 1. 如果 cityId 为空，则不限制城市进行搜索
 *                 2. 如果 hospital 为空，则不搜索医院名称
 *                 3. 如果 department 为空，则不搜索科室名称
 *                 4. 其中三个字段都不是必须字段
 *                 5. 医院名称 和 科室名称 都是需要支持模糊搜索的
 */
router.get('/hospital/search', function (req, res, next) {});

/**
 * @apiName 懒加载模糊搜索医院科室接口
 * @param { int }      cursor       即将获取的科室ID
 * @param { int }      limit        即将获取的科室数量
 * @param { int }      cityId       城市ID - 主键
 * @param { string }   hospital     医院名称
 * @param { string }   department   科室名称
 * @pseudocode 运行逻辑:
 *                 1. 如果 cityId 为空，则不限制城市进行搜索
 *                 2. 如果 hospital 为空，则不搜索医院名称
 *                 3. 如果 department 为空，则不搜索科室名称
 *                 4. 其中 cityId, hospital, department 字段都不是必须字段
 *                 5. 医院名称 和 科室名称 都是需要支持模糊搜索的
 *                 6. 只是在搜索接口的基础上增加了懒加载的 cursor 和 limit 而已
 */
router.get('/hospital/searchLazyload', function (req, res, next) {});

/**
 * @apiName 懒加载模糊搜索医院科室接口
 * @param { int }      pn           分页的页数(从1算起)
 * @param { int }      ps           每一页的数量
 * @param { int }      cityId       城市ID - 主键
 * @param { string }   hospital     医院名称
 * @param { string }   department   科室名称
 * @pseudocode 运行逻辑:
 *                 1. 如果 cityId 为空，则不限制城市进行搜索
 *                 2. 如果 hospital 为空，则不搜索医院名称
 *                 3. 如果 department 为空，则不搜索科室名称
 *                 4. 其中 cityId, hospital, department 字段都不是必须字段
 *                 5. 医院名称 和 科室名称 都是需要支持模糊搜索的
 *                 6. 只是在搜索接口的基础上增加了分页逻辑的 pn 和 ps 而已
 */
router.get('/hospital/searchPaged', function (req, res, next) {});

/**
 * @apiName 获取科室下所有设备接口
 */
router.get('/getAllDevice', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /hospital/getAllDevice`);
    let department;
    const { uid } = req.token_payload.data;
    // 读取科室数据
    try {
        department = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "科室Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 科室Id ${ JSON.stringify(department) }`);
    // 进入业务层
    hospitalService.getAllDevice(req.requestId, uid, department).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取科室里所有设备响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取科室里所有设备时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

module.exports = router;